package la.bbs558.controller.user;

import cn.dev33.satoken.annotation.SaIgnore;
import la.bbs558.aop.ratelimit.LimitType;
import la.bbs558.aop.ratelimit.RateLimiter;
import la.bbs558.aop.valid.Check;
import la.bbs558.aop.valid.Valid;
import la.bbs558.controller.base.BaseController;
import la.bbs558.controller.base.Result;
import la.bbs558.entity.vo.BbsBoardVO;
import la.bbs558.entity.enums.OssFolder;
import la.bbs558.entity.params.*;
import la.bbs558.service.BbsBoardService;
import la.bbs558.service.BbsPostService;
import la.bbs558.service.BbsReplyService;
import la.bbs558.util.Objs;
import la.bbs558.util.OssUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Classname UserController
 * @Description 用户帖子
 * @Date 2023/12/15 10:27
 * @Created by weihanmin
 */
@RestController
@RequestMapping("bbs")
@Check
@RequiredArgsConstructor
@Validated
public class BbsController extends BaseController {
    private final BbsPostService bbsPostService;
    private final BbsBoardService bbsBoardService;
    private final BbsReplyService bbsReplyService;
    private final OssUtil ossUtil;

    @GetMapping("boardInfo")
    @SaIgnore
    public Result<?> boardInfo(Long id) {
        BbsBoardVO res = bbsBoardService.getBoardById(id, false);
        return Result.success(res);
    }

    @GetMapping("postList")
    @SaIgnore
    public Result<?> postList(@Valid BbsPostPage page) {
        Objs.valid(page.getClassId(), "查询条件不能为空");
        bbsPostService.postList(page);
        return Result.success(page);
    }

    @GetMapping("topPostList")
    @SaIgnore
    public Result<?> topPostList(BbsPostPage page) {
        page.setClassId(-3L);
        page.setCurrent(1);
        bbsPostService.postList(page);
        return Result.success(page.getRecords());
    }

    @GetMapping("topSectionPostList")
    @SaIgnore
    public Result<?> topSectionPostList(BbsSectionPostPage page) {
        bbsPostService.topSectionPostList(page);
        return Result.success(page);
    }

    @GetMapping("postDetail")
    @SaIgnore
    public Result<?> postDetail(Long postId) {
        return Result.success(bbsPostService.getPostDetail(postId));
    }

    @GetMapping("postReplyList")
    @SaIgnore
    public Result<?> postReplyList(BbsPostDetailPage page) {
        bbsReplyService.postReplyList(page);
        return Result.success(page);
    }

    @GetMapping("postReplyListLastPage")
    @SaIgnore
    public Result<?> postReplyListLastPage(BbsPostDetailPage page) {
        bbsReplyService.postDetailLastPage(page);
        return Result.success(page);
    }

    @PostMapping("post")
    @RateLimiter(key = "post:",time = 1,count = 1, limitType = LimitType.USER_ID)
    public Result<?> post(@RequestBody @Valid BbsPostParams params) {
        params.setNeedAudit(false);
        bbsPostService.post(params);
        return Result.success();
    }

    @PostMapping("upload")
    @RateLimiter(key = "upload:",time = 1,count = 1, limitType = LimitType.USER_ID)
    public Result<?> upload(MultipartFile file) {
        return Result.success(ossUtil.uploadFile(file, OssFolder.BBS));
    }

    @PostMapping("reply")
    @RateLimiter(key = "reply:",time = 1,count = 1, limitType = LimitType.USER_ID)
    public Result<?> reply(@RequestBody @Valid BbsReplyParams params) {
        bbsReplyService.reply(params);
        return Result.success();
    }

    @PostMapping("likeOrCollect")
    public Result<?> likeOrCollect(@RequestBody @Valid BbsOptParams params) {
        bbsReplyService.likeOrCollect(params);
        return Result.success();
    }

}
